package com.platform.api;

import com.platform.annotation.IgnoreAuth;
import com.platform.entity.SysVersionVo;
import com.platform.service.SysVersionService;
import com.platform.util.ApiBaseAction;
import com.platform.utils.R;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * API登录授权
 *
 * @author 朱如森
 */
@RestController
@RequestMapping("/api/version")
public class ApiVersionController extends ApiBaseAction {


    private Logger logger = Logger.getLogger(getClass());
    @Autowired
    private SysVersionService sysVersionService;


    /**
     */
    @IgnoreAuth
    @RequestMapping("compare")
    public R compare(String mobileType, String version) throws Exception {

        Map<String, Object> params = new HashMap<>();
        params.put("mobileType", mobileType);
        params.put("version", version);
        SysVersionVo sysVersionVo = sysVersionService.selectOne(params);
        if (sysVersionVo == null) {
            return R.ok(1, "当前版本在系统中不存在，请联系客服！", "");
        }
        params = new HashMap<>();
        params.put("mobileType", mobileType);
        params.put("sidx", "update_time");
        params.put("order", "desc");

        // 获取安卓的最新版本
        SysVersionVo newVersion = sysVersionService.selectOne(params);
        if (newVersion != null) {
            if (newVersion.getUpdateTime().getTime() > sysVersionVo.getUpdateTime().getTime()) {
                return R.ok(0, "来点新鲜的，更新到最新版本吧！", newVersion);
            } else {
                return R.ok(1, "已经更新到最新版本啦！", "");
            }
        }


        return R.ok(0, "当前为最新版本", "");
    }
}
